package id.laskarmedia.opnvpn_flutter.handler.OC;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;


import id.laskarmedia.opnvpn_flutter.util.ConnectStateHelper;
import id.laskarmedia.opnvpn_flutter.ContextHelper;
import id.laskarmedia.opnvpn_flutter.enums.VPNEventEnum;
import id.laskarmedia.opnvpn_flutter.handler.AbstractEventHandler;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * InitializeEventHandler class is a concrete implementation of AbstractEventHandler
 * specifically for handling VPN initialization events.
 */
public class InitializeEventHandler extends AbstractEventHandler {

    /**
     * Returns the VPN event type handled by this event handler.
     *
     * @return The VPNEventEnum.INITIALIZE representing the VPN initialization event.
     */
    @Override
    public VPNEventEnum getVPNEvent() {
        return VPNEventEnum.INITIALIZE;
    }

    /**
     * Handles the method call for VPN initialization event.
     *
     * @param call   The method call received from Flutter.
     * @param result The result object to send the method call result back to Flutter.
     */
    @Override
    public void handler(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (ContextHelper.getContext() == null) {
            result.error("-2", "Initialization failed because context is null", "");
            return;
        }
        LocalBroadcastManager.getInstance(ContextHelper.getContext()).registerReceiver(broadcastReceiver, new IntentFilter("connectionState"));
    }

    /**
     * BroadcastReceiver for receiving connection state updates.
     */
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                Log.v("CHECKSTATE", intent.getStringExtra("state"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                String state = intent.getStringExtra("state");
                Long connected_on = null;
                if(!TextUtils.isEmpty(state)){
                    if(VPNEventEnum.CONNECT.getMsg().equals(state.toLowerCase())){
                        connected_on = System.currentTimeMillis();
                    }
                }
                updateState(state);
                String duration = intent.getStringExtra("duration");
                String lastPacketReceive = intent.getStringExtra("lastPacketReceive");
                String byteIn = intent.getStringExtra("byteIn");
                String byteOut = intent.getStringExtra("byteOut");

                if (duration == null) duration = "00:00:00";
                if (lastPacketReceive == null) lastPacketReceive = "0";
                if (byteIn == null) byteIn = "0";
                if (byteOut == null) byteOut = "0";

                ConnectStateHelper.setConnectStatus(connected_on,byteIn, byteOut);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}